package com.webtest.demo;

import static org.testng.Assert.assertTrue;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.openxmlformats.schemas.presentationml.x2006.main.SldDocument;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.webtest.core.BaseTest;

public class AdminTest extends BaseTest {
	
	private static final String content = null;
	private String title;
	WebDriver driver;
	JavascriptExecutor js = (JavascriptExecutor)driver;
	Actions action = new Actions(driver);
	
	
	@BeforeClass
	public void AdminLogin() {
		webtest.open("/");
		webtest.click("link=登录");
		webtest.type("name=login_name","hxy");
		webtest.type("name=login_pass", "123456");
		webtest.click("link=登录");
		assertTrue(webtest.isDisplayed("link=退出"));
	}
	
	@AfterClass
	public void quit() {
		webtest.click("link=hxy");
		webtest.click("link=登录");
	}
	
	@Test
	public void test1() throws Exception{
		//修改安全与效率
		webtest.click("link=前往修改");
		Thread.sleep(1000);
		
		webtest.click("link=安全与效率");
		Thread.sleep(1000);
		
		webtest.click("xpath='//span[@class=\"switchery switchery-default\"]'");
		Thread.sleep(1000);
		
		String title = (String) js.executeScript("retrun document,title");
		System.out.println(title);
		
		js.executeScript("window.scroll(0,doucment.body.scrollHeight)");
		driver.navigate().refresh();
		js.executeScript("arguments[0].scrollIntoView",
				driver.findElement(By.linkText("huangxinyue")));
		driver.navigate().refresh();
		js.executeScript("window.scrollBy(10,500)");
		Thread.sleep(3000);
		
		js.executeScript("return window.title");
		
		webtest.type("name=met_fd_word", "ahahaha");
		Thread.sleep(1000);
	}
	
	@Test
	public void test2() throws Exception {
		//添加新闻文章
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		
		webtest.click("link=不再提示");
		Thread.sleep(1000);
		
		webtest.click("link=后台");
		Thread.sleep(1000);
		
		webtest.click("link=新闻");
		Thread.sleep(1000);
		
		webtest.clear("link=添加内容");
		Thread.sleep(1000);
		
//		单选
		WebElement mass = driver.findElement(By.name("class1"));
		Select s1 = new Select(mass);
		s1.selectByIndex(1);//2，从0开始计数
		System.out.println(s1.isMultiple());
		Thread.sleep(1000);
		
		webtest.type("name=title", "haha");
		Thread.sleep(1000);
		webtest.click("link=保存");
		Thread.sleep(1000);
		
	}
	
	@Test 
	public void test3() throws InterruptedException {
		//添加会员
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.mouseToElement("link=用户管理");
		webtest.click("link=会员");
		Thread.sleep(1000);
		webtest.click("添加会员");
		Thread.sleep(1000);
		webtest.type("name=username", "dadada");
		Thread.sleep(1000);
		webtest.type("name=password", "111111");
		Thread.sleep(1000);
		webtest.click("link=保存");
		//删除会员
		webtest.click("data-id=10");
	}
	
	@Test
	public void test4() throws Exception{
		//添加产品
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.click("link=产品");
		Thread.sleep(1000);
		webtest.click("link=添加内容");
		//按下control键
		action.keyDown(Keys.CONTROL);
		
//		多选
		WebElement type = driver.findElement(By.name("class"));
		Select s2 = new Select(type);
		s2.selectByValue("笔记本电脑");
		s2.selectByVisibleText("平板电脑");
		//松开control键
		action.keyUp(Keys.CONTROL);
		Thread.sleep(1000);
		
		webtest.type("name=title", "dada电子");
		Thread.sleep(1000);
		webtest.click("link=保存");
		Thread.sleep(1000);
	}
	
	public void test5() throws Exception{
		//导出简历信息
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.click("link=招聘");
		
		//无法定位，js修改元素属性
		WebElement h2 = driver.findElement(By.xpath("//div[@id='job-list-43--_wrapper']/table/tbody/tr[0]/td[7]"));
		js.executeScript("arguments[0].setAttribute('class','hxy');",h2);
		
		webtest.click("xpath='a[@class=\"hxy\"]'");
		Thread.sleep(1000);
		
	}
	
	public void test6() throws Exception{
		//删除简历信息
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.click("link=招聘");
		
//		多选
		WebElement num = driver.findElement(By.name("id"));
		Select s2 = new Select(num);
		s2.selectByValue("1");
		s2.selectByVisibleText("2");
		webtest.click("link=删除");
		Thread.sleep(1000);
		webtest.click("link=确定");
		Thread.sleep(1000);
	}
	
	public void test7() throws Exception{
		//大量添加设置留言表单
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.click("link=内容管理");
		Thread.sleep(1000);
		webtest.click("link=留言系统");
		webtest.click("link=留言表单设置");
		Thread.sleep(1000);

		for(int i=2;i<5;i++) {
			webtest.click("link=添加");
			Thread.sleep(1000);
			webtest.type("name=name-new-"+i, "留言"+i+i);
			Thread.sleep(1000);
			
//			单选
			WebElement h1 = driver.findElement(By.name("type-new-"+i));
			Select s1 = new Select(h1);
			s1.selectByIndex(2);//2，从0开始计数
			System.out.println(s1.isMultiple());
			Thread.sleep(1000);
			
//			单选
			WebElement h2 = driver.findElement(By.name("access-new-"+i));
			Select s2 = new Select(h2);
			s2.selectByIndex(1);//2，从0开始计数
			System.out.println(s2.isMultiple());
			Thread.sleep(1000);
		}
		webtest.click("link=保存");
	}
	
	@Test
	public void test8() throws Exception {
		//添加招聘职位
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.click("link=内容管理");
		webtest.click("link=加入我们");
		Thread.sleep(1000);
		webtest.click("link=添加内容");
		Thread.sleep(1000);
		webtest.type("name=position", "测试经理");
		webtest.type("name=place", "河北省石家庄市");
		webtest.type("name=deal", "10k-18k");
		webtest.type("name=count", "3");
		webtest.type("name=useful_life", "1个月");
		Thread.sleep(1000);
		webtest.click("link=保存");
		Thread.sleep(1000);
		
	}
	
	@Test
	public void test9() throws Exception{
		//修改会员组
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.mouseToElement("link=用户管理");
		webtest.click("link=会员");
		webtest.click("link=会员组");
		webtest.click("value=普通会员");
		webtest.type("value=普通会员", "01");
		Thread.sleep(1000);
		webtest.click("link=保存");
		Thread.sleep(1000);
		
	}
	
	@Test
	public void test10() throws Exception{
		//添加管理员
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.mouseToElement("link=用户管理");
		webtest.click("link=管理员");
		webtest.click("link=添加管理员");
		webtest.type("name=admin_id", "hhh");
		Thread.sleep(1000);
		webtest.type("admin_pass", "123456");
		Thread.sleep(1000);
		webtest.type("name=admin_pass_replay", "123456");
		Thread.sleep(1000);
		webtest.click("link=内容管理员");
		Thread.sleep(1000);
		webtest.click("link=保存");
		Thread.sleep(1000);
		
	}
	
	@Test
	public void test11() throws Exception{
		//添加新语言
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.click("link=多语言");
		Thread.sleep(1000);
		webtest.click("link=添加新语言");
		Thread.sleep(1000);
		webtest.type("name=order", "5");
//		单选
		WebElement h1 = driver.findElement(By.name("autor"));
		Select s1 = new Select(h1);
		s1.selectByIndex(15);//2，从0开始计数
		System.out.println(s1.isMultiple());
		Thread.sleep(1000);
		webtest.click("link=保存");
		Thread.sleep(1000);
	}
	
	@Test
	public void test12() throws Exception{
		//设置网站的基本信息
		webtest.click("link=传统后台");
		Thread.sleep(1000);
		webtest.click("link=不再提示");
		webtest.mouseToElement("link=基本设置");
		webtest.click("link=基本信息");
		Thread.sleep(1000);
		webtest.type("name=met_webname", "dada网站");
		Thread.sleep(1000);
		//无法定位，js修改元素属性
		WebElement h1 = driver.findElement(By.xpath("//div[@class='metadmin-fmbx']/dl[1]/dd/div/span/div[2]/div[1]/div/label"));
		js.executeScript("arguments[0].setAttribute('class','hxy111');",h1);
		
		webtest.type("xpath='a[@class=\"hxy111\"]'","C:\\Users\\19646\\Pictures\\Saved Pictures\\兔子.jpg");
		Thread.sleep(1000);
		webtest.click("link=保存");
		Thread.sleep(1000);
		
	}

}
